﻿@{
    Layout = "_ContentLayout.cshtml";
}

@section headerBody {
    <h1>Installing web services and deploying a web application to a remote server</h1>
    <p class="lead">Sometimes a package may contain more than just a nuspec file and an install script. You may also want to install the package onto a remote machine.</p>
    <p class="lead"><span class="text-primary">Boxstarter can create a template package targeting an existing directory and install the package on any machine open to WINRM or WMI remoting.</span></p>
}
<p>This walk through will create a Nuget ODATA feed application and deploy it to a remote server after enabling web services on the server.</p>

<h3>Step 1</h3>
<h4><span class="text-primary">Create and Build the Nuget server</span></h4>
<p>Create an empty Web Application in Visual Studio and add a Nuget reference to Nuget.Server. After the package install completes, build the solution. NOTE: This can be performed with the free Visual Studio Express for Web which can be installed via Chocolatey - <code>cinst VisualStudio2013ExpressWeb</code>.</p>
<img src="~/Images/NugetServer.png">

<h3>Step 2</h3>
<h4><span class="text-primary">Install Boxstarter</span></h4>
<p>If you have Chocolatey:</p>
<pre>CINST Boxstarter</pre>
<p>Otherwise, click on the download link at the top of this page and run the Setup.bat file. See <a href="InstallBoxstarter">Installing Boxstarter</a> for details.</p>

<h3>Step 3</h3>
<h4><span class="text-primary">Invoke the Boxstarter shell</span></h4>
<img src="~/Images/shortcut.png">
<p>The Boxstarter install creates a shortcut to a shell that provides the easiest way to access Boxstarter commands especially if you are not familiar with Powershell. See <a href="~/UsingBoxstarter">Using Boxstarter Commands</a> for information about running Boxstarter in your own shell.</p>

<h3>Step 4</h3>
<h4><span class="text-primary">Create the Package</span></h4>
<p>Lets assume that you saved the above Visual Studio project to c:\dev\NugetServer. You can now create a package with the artifacts of this project embedded in the package:</p>
<pre>
New-BoxstarterProject -Name NugetFeedApp
Copy-Item c:\dev\NugetServer "$($Boxstarter.LocalRepo)\NugetServer" -Recurse -Force
</pre>
<p>This creates a skeleton Chocolatey package with the contents of your NugetServer application embedded in the package. You will need to complete the ChocolateyInstall.ps1 file.</p>

<h3>Step 5</h3>
<h4><span class="text-primary">Add installation details to ChocolateyInstall.ps1</span></h4>
<p>Boxstarter has created a skeletal ChocolateyInstall.ps1 file at $($Boxstarter.LocalRepo)\NugetServer\tools\chocolateyInstall.ps1. Edit this file and replace its contents with the following script:</p>
<pre>
try {
    cinst DotNet4.5

    #Enable Web Services
    cinst IIS-WebServerRole -source WindowsFeatures
    cinst IIS-ISAPIFilter -source WindowsFeatures
    cinst IIS-ISAPIExtensions -source WindowsFeatures

    #Enable ASP.NET on win 2012/8
    cinst IIS-NetFxExtensibility45 -source WindowsFeatures
    cinst NetFx4Extended-ASPNET45 -source WindowsFeatures
    cinst IIS-ASPNet45 -source WindowsFeatures

    #Enable ASP.NET on win 7/2008R2
    ."$env:windir\microsoft.net\framework\v4.0.30319\aspnet_regiis.exe" -i

    #clean and create application
    Remove-Item c:\web\NugetServer -Recurse -Force -ErrorAction SilentlyContinue
    Mkdir c:\web\NugetServer -ErrorAction SilentlyContinue
    Copy-Item "$(Join-Path (Get-PackageRoot $MyInvocation ) NugetServer)\*" c:\web\NugetServer -Recurse -Force
    Import-Module WebAdministration
    Remove-WebSite -Name "Default Web Site" -ErrorAction SilentlyContinue
    Remove-WebSite -Name NugetServer -ErrorAction SilentlyContinue
    New-WebSite -ID 1 -Name NugetServer -Port 80 -PhysicalPath c:\web\NugetServer -Force

    #Client SKUs need to enable firewall
    netsh advfirewall firewall add rule name="Open Port 80" dir=in action=allow protocol=TCP localport=80
    
    Write-ChocolateySuccess 'NugetServer'
} catch {
  Write-ChocolateyFailure 'NugetServer' $($_.Exception.ToString())
  throw
}
</pre>
<p>This script enables the minimum IIS features for the application to run, copies the application to the computer installing the package, creates a web application in IIS to point to the application directory and allows http traffic through the firewall.</p>

<h3>Step 6</h3>
<h4><span class="text-primary">Build the package</span></h4>
<p>We need to build our package script and application files into a single .nupkg file. Run:</p>
<pre>Invoke-BoxstarterBuild -Name NugetServer</pre>

<h3>Step 7</h3>
<h4><span class="text-primary">Ensure Powershell Remoting is enabled on the target machine</span></h4>
<p>In many cases this step is unnecessary. If you are using either the builtin administrator account or a domain account with admin privileges on a server operating system, remoting is on by default. Otherwise, run the following command on the machine with a user who has admin privileges:</p>
<pre>Enable-PSRemoting -Force</pre>

<h3>Step 8</h3>
<h4><span class="text-primary">Install the package on the target</span></h4>
<p>Now we are ready to enable IIS services and deploy the application. We will create a PSCredential object and invoke the install:</p>
<pre>
$Creds = Get-Credential administrator
Install-BoxstarterPackage -ComputerName MyMachine -PackageName NugetServer -Credential $creds -Force
</pre>

<h3>Step 9</h3>
<h4><span class="text-primary">Run the application</span></h4>
<p>Now we should be able to browse to our Nuget server application at http://MyServer</p>
<img src="~/Images/NugetServerWeb.png">
